import 'package:edu/constants/app_strings.dart';
import 'package:edu/networks/course/models/course_response.dart';
import 'package:flutter/material.dart';

class CourseBrief extends StatelessWidget {
  final CourseDetail courseDetail;

  const CourseBrief({super.key, required this.courseDetail});

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      key: const PageStorageKey('brief'),
      slivers: [
        SliverToBoxAdapter(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildCourseDescription(context),
              _buildCourseSkills(context),
              _buildAuthorIntroduction(context),
            ],
          ),
        ),
      ],
    );
  }

  // 课程简介
  Widget _buildCourseDescription(BuildContext context) {
    final v = courseDetail.description;
    final titleStyle = Theme.of(
      context,
    ).textTheme.titleMedium?.copyWith(fontWeight: FontWeight.bold);
    final bodyStyle = Theme.of(
      context,
    ).textTheme.bodyMedium?.copyWith(height: 1.75);

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 20),
      color: Theme.of(context).colorScheme.surface.withAlpha(128),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const SizedBox(height: 15),
          Text(AppStrings.courseDescription, style: titleStyle),
          const SizedBox(height: 5),
          Text(v, style: bodyStyle),
          const SizedBox(height: 15),
        ],
      ),
    );
  }

  // 可以学到的内容
  Widget _buildCourseSkills(BuildContext context) {
    final v = courseDetail.skills;
    final titleStyle = Theme.of(
      context,
    ).textTheme.titleMedium?.copyWith(fontWeight: FontWeight.bold);
    final bodyStyle = Theme.of(
      context,
    ).textTheme.bodyMedium?.copyWith(height: 1.75);
    var index = 1;
    final skills = v
        .map((e) => TextSpan(text: '${index++}、$e\n', style: bodyStyle))
        .toList();
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const SizedBox(height: 15),
          Text(AppStrings.courseSkills, style: titleStyle),
          const SizedBox(height: 5),
          RichText(text: TextSpan(children: skills)),
        ],
      ),
    );
  }

  // 讲师介绍
  Widget _buildAuthorIntroduction(BuildContext context) {
    final v = courseDetail.author.introduction;
    final titleStyle = Theme.of(
      context,
    ).textTheme.titleMedium?.copyWith(fontWeight: FontWeight.bold);
    final bodyStyle = Theme.of(
      context,
    ).textTheme.bodyMedium?.copyWith(height: 1.75);

    if (v != null && v.isNotEmpty) {
      return Container(
        padding: const EdgeInsets.symmetric(horizontal: 20),
        color: Theme.of(context).colorScheme.surface.withAlpha(128),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(height: 15),
            Text(AppStrings.authorIntroduction, style: titleStyle),
            const SizedBox(height: 5),
            Text(v, style: bodyStyle),
            const SizedBox(height: 15),
          ],
        ),
      );
    } else {
      return const Center(child: Text('暂无该课程讲师介绍'));
    }
  }
}
